package com.admin.creditcard.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.admin.creditcard.domain.CreditCard;
import com.admin.creditcard.domain.CreditElectronic;
import com.admin.creditcard.feignService.CreditFeignFromBank;
import com.admin.creditcard.mapper.CreditCardMapper;
import com.admin.creditcard.mapper.CreditElectronicMapper;
import com.admin.creditcard.feignService.CreditFeignFromUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.admin.creditcard.domain.CreditRecord;
import com.admin.creditcard.service.CreditRecordService;
import com.admin.creditcard.mapper.CreditRecordMapper;
import com.client.common.config.IdWorker;
import com.client.model.pojo.bank.ArElectronic;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Administrator
 * @description 针对表【credit_record】的数据库操作Service实现
 * @createDate 2024-04-12 15:12:08
 */
@Slf4j
@Service
public class CreditRecordServiceImpl extends ServiceImpl<CreditRecordMapper, CreditRecord>
        implements CreditRecordService {
    @Autowired
    private CreditRecordMapper creditRecordMapper;
    @Autowired
    private CreditCardMapper creditCardMapper;
    @Autowired
    private CreditElectronicMapper creditElectronicMapper;

    @Autowired
    private CreditFeignFromUser creditFeignFromUser;
    @Autowired
    private CreditFeignFromBank creditFeignFromBank;
    @Override
    public boolean refuseTheApply(Long id) {
        log.info("id------------->{}",id);
        CreditRecord creditRecord = creditRecordMapper.selectById(id);
        if (creditRecord.getIsAccess() == 0) {
            creditRecord.setIsAccess(2);
            int i = creditRecordMapper.updateById(creditRecord);
            if (i == 1) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Result<String> successApply(Long id) {
        log.info("id------------->{}",id);
        CreditRecord creditRecord = this.creditRecordMapper.selectById(id);
        log.info("用户的申请记录信息------------》{}",creditRecord);
        //申请通过
        creditRecord.setIsAccess(1);
        creditRecord.setUpdateTime(new Date());
        this.creditRecordMapper.updateById(creditRecord);

        //根据creditRecord中的身份证号和iden来查询用户表中当前用户的数据
        ArUser creditUser = this.creditFeignFromUser.findCreditUser(creditRecord.getIden(), creditRecord.getMobile());
        log.info("找寻到的申请信用卡用户的信息----->{}",creditUser);
        //根据uuid生成随机的信用卡卡号，设置还款日期为下个月的今天，借款金额设为0元，uid设置为用户表中的主键
        CreditCard creditCard=new CreditCard();
        String uuid = UUID.randomUUID().toString();
        creditCard.setCardNum(uuid);
        creditCard.setBorrowCount(new BigDecimal(0));
        creditCard.setBorrowDate(DateUtil.nextMonth());
        creditCard.setUid(creditUser.getUid());
        int insert = this.creditCardMapper.insert(creditCard);
        if (insert!=1){
            throw new RuntimeException("信用卡申请失败");
        }
        //根据当前的用户信息去查询二类卡账户的卡的信息   设置用户的主键、二类账户的主键、信用卡的主键
        ArElectronic arElectronic = this.creditFeignFromBank.applyCreditCard(creditUser.getUid());
        CreditElectronic creditElectronic=new CreditElectronic();
        IdWorker idWorker = new IdWorker();
        creditElectronic.setId(idWorker.nextId());
        creditElectronic.setEid(arElectronic.getEid());
        creditElectronic.setCid(creditCard.getId());
        creditElectronic.setUid(creditUser.getUid());
        int insert1 = this.creditElectronicMapper.insert(creditElectronic);
        if (insert1!=1){
            throw new RuntimeException("信用卡申请失败");
        }else {
            return Result.build("申请成功！",200,"申请成功！");
        }
    }
}




